package com.vue.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vue.bean.R;
import com.vue.entity.DictDataEntity;
import com.vue.mapper.DictDataMapper;
import com.vue.service.IDictDataService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * <p>
 * 基础数据表 服务实现类
 * </p>
 *
 * @author wangruijie
 * @since 2024-08-04
 */
@Slf4j
@Service
@AllArgsConstructor
public class DictDataServiceImpl extends ServiceImpl<DictDataMapper, DictDataEntity> implements IDictDataService {

    @Override
    public List<DictDataEntity> selectList(DictDataEntity po) {
        QueryWrapper<DictDataEntity> queryWrapper = new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(po.getDictType())){
            queryWrapper.lambda().like(DictDataEntity::getDictType, po.getDictType());
        }
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public R addOrEditDictData(DictDataEntity po) {
        if (po.getId() == null){
            baseMapper.insert(po);
        }else {
            baseMapper.updateById(po);
        }
        return R.data(po);
    }

    @Override
    public R deleteById(Long id) {
        DictDataEntity entity = baseMapper.selectById(id);
        if (entity == null){
            return R.fail("数据不存在");
        }
        removeById(id);
        return R.status(true);
    }

    @Override
    public R selectById(Long id) {
        return R.data(getById(id));
    }
}
